app.controller('shopDeliveryEditCtrl', ['$scope', '$state', '$cookies', '$modal', '$stateParams','shopDeliveryEditService', 'utilService', 'toaster', function ($scope, $state, $cookies, $modal,$stateParams, shopDeliveryService, utilService, toaster) {

    $scope.shopDelivery = {};

    $scope.id = $stateParams.id;

    $scope.inint = function () {
        $scope.query();
    }

    $scope.query = function () {
        shopDeliveryService.query($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shopDelivery = result.data;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.edit = function () {
        if($scope.shopDelivery.minDelivery > $scope.shopDelivery.maxDelivery){
            toaster.pop('error', "操作失败", "最大配送距离必须大于最小配送距离");
            return;
        }
        delete $scope.shopDelivery.createdTime;
        delete $scope.shopDelivery.createdBy;
        delete $scope.shopDelivery.updatedTime;
        delete $scope.shopDelivery.updatedBy;
        delete $scope.shopDelivery.isDelete;
        shopDeliveryService.edit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.shopDelivery');
                    break;
                default:
                    if (result.message != null) {
                        toaster.pop('error', "操作失败", result.message);
                    } else {
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    }
                    break;
            }
        });
    }

}]);
